// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        className: "LoginScene",
        btn_wechat: {
            default: null,
            type: cc.Button
        },
        btn_youke: {
            default: null,
            type: cc.Button
        },
        editbox: {
            default: null,
            type: cc.EditBox
        },
        pk_Prefab: {
            default: null,
            type: cc.Prefab
        },
    },
    ctor: function () {
        cc.director.setDisplayStats(false);
        // let Card = function (id) {
        //     let color = Math.floor(id / 13);
        //     let value = id % 13;
        //     return {
        //         id: id,
        //         value: value,
        //         color: color,
        //         sort: 13 - value,
        //         des: ["黑桃", "红桃", "梅花", "方块"][color] + ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"][value]
        //     }
        // };
        // for (var i = 0; i < 54; i++) {
        //     console.log(Card(i));
        // }
    },
    onWechat: function () {
        console.log("onWechat..." + this.editbox.string);
    },
    onYouke: function () {

        /*
        let Card = function (id) {
            let color = Math.floor(id / 13);
            let value = id % 13;
            return {
                id: id,
                value: value,
                color: color,
                sort: 13 - value,
                des: ["黑桃", "红桃", "梅花", "方块"][color] + ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"][value]
            }
        };

        var isDouble = function (cardList) {
            let map = {};
            for (let i = 0; i < cardList.length; i++) {
                let card = cardList1[i];
                let value = card.value;
                map[value] = true;
            }
            if (Object.keys(map).length === 2) {
                return true;
            }
            return false;
        };

        var isChain = function (cardList) {
            let values = [];
            for (let i = 0; i < cardList.length; i++) {
                values.push(cardList[i].value);
            }
            values.sort(function (a, b) {
                return a - b;
            });
            if (values[0] + 1 === values[1] && values[1] + 1 === values[2]) {
                return true;
            }
            return false;
        };

        var isSameColor = function (cardList) {
            let map = {};
            for (let i = 0; i < cardList.length; i++) {
                map[cardList[i].color] = true;
            }
            return Object.keys(map).length === 1;
        }

        var isSameColorChain = function (cardList) {
            if (isSameColor(cardList) && isChain(cardList)) {
                return true;
            }
            return false;
        }

        var isBoss = function (cardList) {
            let map = {};
            for (let i = 0; i < cardList.length; i++) {
                map[cardList[i].value] = true;
            }
            return Object.keys(map).length === 1;
        }

        let checkFuntion = {
            isDouble: isDouble,
            isChain: isChain,
            isSameColor: isSameColor,
            isSameColorChain: isSameColorChain,
            isBoss: isBoss
        };
        let CardType = {
            Single: 0,
            Double: 1,
            Chain: 2,
            SameColor: 3,
            SameColorChain: 4,
            Boss: 5
        };
        let checkResult = {
            isSingle: CardType.Single,
            isDouble: CardType.Double,
            isChain: CardType.Chain,
            isSameColor: CardType.SameColor,
            isSameColorChain: CardType.SameColorChain,
            isBoss: CardType.Boss
        };

        let getResult = function (cardList1, cardList2) {
            let result = { 0: 0, 1: 0 };
            let cards = [cardList1, cardList2];
            for (let i = 0; i < 2; i++) {
                for (let key in checkFuntion) {
                    if (checkFuntion[key](cards[i])) {
                        result[i] = checkResult[key];
                    }
                }
            }

            let sortCard = function (cards) {
                if (isDouble(cards)) {
                    let map = {};
                    for (let i = 0; i < cards.length; i++) {
                        let card = cards[i];
                        if (map.hasOwnProperty(card.value)) {
                            map[card.value].push(card);
                        } else {
                            map[card.value] = [card];
                        }
                    }
                    let singleValue = 0;
                    for (let value in map) {
                        if (map[value].length === 1) {
                            singleValue = parseInt(value);
                        }
                    }
                    cards.sort(function (a, b) {
                        return a.value === singleValue;
                    });
                } else {
                    cards.sort(function (a, b) {
                        return a.value > b.value;
                    });
                }
                return cards;
            };
            cardList1 = sortCard(cardList1);
            cardList2 = sortCard(cardList2);
            if (result[0] === result[1]) {
                switch (result[0]) {
                    case CardType.Single:
                        // 单牌只比较牌面值(value)，不比较花色(color)
                        return cardList1[2].value > cardList2[2].value;
                        break;
                    case CardType.Double:
                        // 先比对子牌、再比单牌
                        if (cardList1[0].value === cardList2[0].value) {
                            return cardList1[2] > cardList2[2];
                        }
                        return cardList1[0].value > cardList2[0].value;
                        break;
                    case CardType.Chain:
                        // 只比最大或最小的牌
                        if (cardList1[0].value === cardList2[0].value) {
                            return cardList1[2].color < cardList2[2].color;
                        }
                        return cardList1[0].value > cardList2[0].value;
                        break;
                    case CardType.SameColor:
                        if (cardList1[0].color === cardList2[0].color) {
                            return cardList1[0].value > cardList2[0].value;
                        }
                        return cardList1[0].color < cardList2[0].color;
                        break;
                    case CardType.SameColorChain:
                        return cardList1[0].value < cardList2[0].value;
                        break;
                    case CardType.Boss:
                        return cardList1[0].value > cardList2[0].value;
                        break;
                    default:
                        break;
                }
            }
            return result[0] > result[1];
        };
        let cardList1 = [1, 2, 4];
        let cardList2 = [22, 21, 24];
        let cardList3 = [1, 1, 1];
        let cardList4 = [0, 1, 15];
        let cardList5 = [15, 15, 14];
        for (let i = 0; i < cardList1.length; i++) {
            cardList1[i] = Card(cardList1[i]);
            cardList2[i] = Card(cardList2[i]);
            cardList3[i] = Card(cardList3[i]);
            cardList4[i] = Card(cardList4[i]);
            cardList5[i] = Card(cardList5[i]);
        }
        // console.log(cardList1);
        // console.log(cardList2);
        // console.log(cardList3);
        // console.log(cardList4);
        // console.log(cardList5);
        // console.log("是否是对子: " + isDouble(cardList1));
        // console.log("是否是链子: " + isChain(cardList2));
        // console.log("是否是金花: " + isSameColor(cardList3));
        // console.log("是否是顺金: " + isSameColorChain(cardList4));
        // console.log("是否是顺子: " + isChain(cardList4));
        // console.log("是否是炸弹: " + isBoss(cardList5));
        console.log("result = ", getResult(cardList1, cardList2));
        return;

        //洗牌 //抽牌法
        var shuffle_pick_1 = function (m) {
            //生成m张牌
            var arr = new Array(m);
            for (var i = 0; i < m; i++) {
                arr[i] = i;
            }
            //每次抽出一张牌，放在另一堆。因为要在数组里抽出元素，把后面的所有元素向前拉一位，所以很耗时。
            var arr2 = new Array();
            for (var i = m; i > 0; i--) {
                var rnd = Math.floor(Math.random() * i);
                arr2.push(arr[rnd]);
                arr.splice(rnd, 1);
            }
            return arr2;
        }
        //洗牌 //插牌法
        var shuffle_insert_1 = function (m) {
            //每次生成一张最大的牌，插在随机的某张牌前。因为要在数组里插入元素，把后面的所有元素向后挤一位，所以很耗时。
            var arr = [0];
            for (var i = 1; i < m; i++) {
                arr.splice(Math.floor(Math.random() * (i + 1)), 0, i);
            }
            return arr;
        }
        // 1、抽牌法
        var shuffle_pick = function (m) {
            //生成m张牌
            let arr = new Array(m);
            for (let i = 0; i < m; i++) {
                arr[i] = i;
            }
            //每次抽出一张牌，放在另一堆。把最后一张未抽的牌放在空位子上。
            let cards = new Array();
            for (let i = m; i > 0;) {
                let rnd = Math.floor(Math.random() * i);
                cards.push(arr[rnd]);
                arr[rnd] = arr[--i];
            }
            return cards;
        }


        // 2、换牌法
        var shuffle_swap = function (m) {
            //生成m张牌
            var arr = new Array(m);
            for (var i = 0; i < m; i++) {
                arr[i] = i;
            }
            //第i张与任意一张牌换位子，换完一轮即可
            for (var i = 0; i < m; i++) {
                var rnd = Math.floor(Math.random() * (i + 1)),
                    temp = arr[rnd];
                arr[rnd] = arr[i];
                arr[i] = temp;
            }
            return arr;
        }

        //洗牌 // 插牌法优化版，可以用数学归纳法证明，这种洗牌是均匀的。
        var shuffle_insert = function (m) {
            //每次生成一张最大的牌，与随机的某张牌换位子
            var arr = new Array(m);
            arr[0] = 0;
            for (var i = 1; i < m; i++) {
                var rnd = Math.floor(Math.random() * (i + 1));
                arr[i] = arr[rnd];
                arr[rnd] = i;
            }
            return arr;
        }

        var funcs = [shuffle_pick_1, shuffle_pick, shuffle_swap, shuffle_insert_1, shuffle_insert];
        var funcNames = ["抽牌", "抽牌优化", "换牌", "插牌", "插牌优化"];
        var m = 10000;
        for (var i = 0; i < funcs.length; i++) {
            var d0 = new Date();
            funcs[i](m);
            funcNames[i] = (new Date() - d0) + '\t' + funcNames[i];
        }
        console.log(funcNames);
*/
        var userid = this.editbox.string;
        if (userid === "") {
            var pk = cc.instantiate(this.pk_Prefab);
            pk.parent = this.node;
            return;
        };
        network.addListener(this.className, "login", function (data) {
            DataManager.ownerUid = data.uid;
            // 切换场景
            var onSceneLaunched = function () {
                console.log("onSceneLaunched");
            };
            // cc.director.loadScene("HomeScene", onSceneLaunched);

            // 预加载场景
            cc.director.preloadScene("HomeScene", function () {
                cc.director.loadScene("HomeScene", onSceneLaunched);
                console.log("Next scene HomeScene preloaded");
            });
        });
        // var url = "192.168.199.203:3000";
        // var url = "localhost:3000";
        var url = "193.112.243.99:3000";
        network.connect(url, function () {
            network.send("login", { unionid: parseInt(userid), nickname: userid });
        });
    },
    onLoad() {
        this.btn_wechat.node.active = false;
        this.btn_youke.node.x = 0;
    },

    start() {

    },

    // update (dt) {},
});
